//用于自我
function DataAdd(){//属性增加
	setInterval(function(){
		//灵气增加
		var MyEnergy = Number(plus.storage.getItem("MyEnergy"));//获取当前能量
		MyEnergy += MyEnergyAdd();//灵力增加
		
		if(Number(plus.storage.getItem("MyForgeExpUp"))>0){//锻造的装备经验加成
			MyEnergy += Number(plus.storage.getItem("MyForgeExpUp"));
		}
		
		plus.storage.setItem("MyEnergy",String(MyEnergy));
		document.getElementById("MyEnergy").innerHTML = plus.storage.getItem("MyEnergy");
		
		plus.storage.setItem("MyVim" , String( Number(plus.storage.getItem("MyVim")) + MyRealmUpLifeUp(plus.storage.getItem("Race"),plus.storage.getItem("MyRealm"))));
		document.getElementById("MyVim").innerHTML = plus.storage.getItem("MyVim");
		if(plus.storage.getItem("MyAge")<=0){//年龄增减
			if(plus.storage.getItem("OneDie") == null){
				var d = new Date();
				var x = d.toLocaleDateString();
				plus.storage.setItem("OneDie",x);
			}
			plus.storage.setItem("YouDie","1");
			window.location.href='../SoulWorld/home.html';
		}



		//领地资源增加
			//领地灵植产生灵气
		plus.storage.setItem("MyAreaHasEnergy",String(Number(plus.storage.getItem("MyAreaHasEnergy"))+Math.floor(Number(plus.storage.getItem())/10)));

		if(MyPeopleNumberAreaDouble()!=0 && Math.floor(Number(plus.storage.getItem("MyAreaBig"))/10) - MyAgeAreaDouble() > 0){//人口增加
			plus.storage.setItem("FanRen",String(Number(plus.storage.getItem("FanRen"))+1));
		}
		if(MyPeopleNumberAreaDouble()!=0 && Math.floor(Number(plus.storage.getItem("MyAreaBig"))/5) - MyAgeAreaDouble() > 0){//灵兽增长
			plus.storage.setItem("MyAreaBeast",String(Number(plus.storage.getItem("MyAreaBeast"))+1));
		}
		if(Number(plus.storage.getItem("MyCopyNum"))>0&&MyPeopleNumberAreaDouble()>0){
			if(MyPeopleAbilityAreaDouble()<Number(plus.storage.getItem("MyCopyNum"))*1000){//秘境资源恢复如果比总战力高
				plus.storage.setItem("MyAreaPlant",String(Number(plus.storage.getItem("MyAreaPlant"))+Math.round(MyPeopleAbilityAreaDouble()*Math.random())));//灵植增长
				plus.storage.setItem("MyAreaBeast",String(Number(plus.storage.getItem("MyAreaBeast"))+Math.round(MyPeopleAbilityAreaDouble()*Math.random())));//灵矿增长
			}else{
				plus.storage.setItem("MyAreaPlant",String(Number(plus.storage.getItem("MyAreaPlant"))+Number(plus.storage.getItem("MyCopyNum"))*1000));
			}
		}
		
		
	},10000);
	
	setInterval(function(){EveryAge()},AgeTime());
	function EveryAge(){
		plus.storage.setItem("MyAge",String(Number(plus.storage.getItem("MyAge"))-1));//寿命流失
		document.getElementById("tip2").innerHTML="又过去了一年,你寿命-1";
		document.getElementById("MyAge").innerHTML=plus.storage.getItem("MyAge");
		mui.toast('过去一年了');
		if(plus.storage.getItem("MyAge")<=0){
			if(plus.storage.getItem("OneDie") == null){
				var d = new Date();
				var x = d.toLocaleDateString();
				plus.storage.setItem("OneDie",x);
			}
			plus.storage.setItem("YouDie","1");
			window.location.href='../SoulWorld/home.html';
		}
		if(Math.floor(Math.random() * (10 + 1))!=0){
			document.getElementById("tip3").innerHTML = "今年无事发生";
		}else{//机遇
			EveryAgeChance();
		}
	}
	function AgeTime(){//寿命消耗时间
		MyAgeTime = 60000;//基础为1分钟
		MyAgeTime *= 1+(0.1*MyAgeAreaDouble());
		return MyAgeTime;
	}
	function EveryAgeChance(){//机遇
		var max = 2;//有多少个事件
		var Random = Math.floor(Math.random() * max) + 1;
		
		switch(Random){
			case 1://获得100s灵气,额外加10s灵气到领地聚灵
				plus.storage.setItem("MyAreaHasEnergy",String(AgeTime()/60000*Number(plus.storage.getItem("MyAreaHasEnergy"))+MyEnergyAdd()));
				var MyEnergy = Number(plus.storage.getItem("MyEnergy"));//获取当前能量
				MyEnergy += AgeTime()/60000 * 10 * MyEnergyAdd();//灵力增加
				plus.storage.setItem("MyEnergy",String(MyEnergy));
				document.getElementById("tip3").innerHTML = "嗯?此地灵气确实不错啊!(获得100s灵气,额外加10s灵气到领地聚灵)";
				break;
			
			case 2://获得50s精力
				plus.storage.setItem("MyVim" , String(5 * AgeTime()/60000 * Number(plus.storage.getItem("MyVim")) + MyRealmUpLifeUp(plus.storage.getItem("Race"),plus.storage.getItem("MyRealm"))));
				document.getElementById("tip3").innerHTML = "游离山水之时,发现一处绝美风景(获得50s精力)";
				break;
			
			case 3:
				break;
			
			default:
				break;
		}
		return false;
	}
}





//数据增加
function MyEnergyAdd(){
	var Race = plus.storage.getItem("Race");
	var myRealm = plus.storage.getItem("MyRealm");
	var EnergyAreaDouble = MyEnergyAreaDouble();
	
	var Add = 1+EnergyAreaDouble;//给出地区灵力加成
	switch (Race){
		case "人"://如果是种族人
			switch(myRealm){
				case "无":
					break;
				case "练气前期":
					Add *=2;
					break;
				case "练气中期":
					Add *=3;
					break;
				case "练气后期":
					Add *=4;
					break;
				case "筑基前期":
					Add *=6;
					break;
				case "筑基中期":
					Add *=8;
					break;
				case "筑基后期":
					Add *=10;
					break;
				case "金丹前期":
					Add *=14;
					break;
				case "金丹中期":
					Add *=18;
					break;
				case "金丹后期":
					Add*=22;
					break;
				case "元婴前期":
					Add *=30;
					break;
				case "元婴中期":
					Add *=40;
					break;
				case "元婴后期":
					Add *= 50;
					break;
				default:
					return "err";
			}
			break;
		default:
			break;
	}
	
	return Add;
}



//境界提升

//突破境界事件
function MyRealmUp(){//Area为地区突破加成
	var MyRace = plus.storage.getItem("Race");
	var NowRealm = plus.storage.getItem("MyRealm");
	var myUpAreaDouble = MyUpAreaDouble();
	var MyEnergy = plus.storage.getItem("MyEnergy");
	
	

	Number(MyEnergy);//数据防止是string类型
	if(MyEnergy<MyRealmUpEnergyHelp(NowRealm)){//如果灵气不够
		return 1;
	}
	EnergyOdds=1;//突破概率
	EnergyOdds += myUpAreaDouble/10;
	if(EnergyOdds<Math.floor(Math.random() * MyRealmUpOdds())+1){//如果突破失败
		return 2;
	}
	return 3;//如果突破成功
}

//突破概率最大数
function MyRealmUpOdds(){
	var MyRace = plus.storage.getItem("Race");
	var NowRealm = plus.storage.getItem("MyRealm");
	
	switch(MyRace){
		case "人":
			switch (NowRealm){
				case "无":
					EnergyUpOdds = 2;
					break;
				case "练气前期":
					EnergyUpOdds = 10;//从这开始
					break;
				case "练气中期":
					EnergyUpOdds = 20;
					break;
				case "练气后期":
					EnergyUpOdds = 40;//以上倍率为2
					break;
					//达到筑基
				case "筑基前期":
					EnergyUpOdds = 120;
					break;
				case "筑基中期":
					EnergyUpOdds = 360;
					break;
				case "筑基后期":
					EnergyUpOdds = 1080;//以上倍率为3
					break;
					//达到金丹
				case "金丹前期":
					EnergyUpOdds = 4320;
					break;
				case "金丹中期":
					EnergyUpOdds = 17280;
					break;
				case "金丹后期":
					EnergyUpOdds = 69120;//以上倍率为4
					break;
					//达到元婴
				case "元婴前期":
					EnergyUpOdds = 414720;//倍率为6
					break;
				case "元婴中期":
					EnergyUpOdds = 4147200;//倍率为10
					break;
				case "元婴后期":
					EnergyUpOdds = 62208000;//*15
					break;		   
				default:
						return "err";
					break;
				}
				break;
			default:alert("突破函数出现异常,请直接关闭游戏,不要点退出,直接关闭重新进来!否则数据可能丢失!");
				break;
	}
	return EnergyUpOdds;
}
//升级所需灵气
function MyRealmUpEnergyHelp(NowRealm){
	var MyRace = plus.storage.getItem("Race");
	
	
	var EnergyHelp;//所需灵气变量
	switch(MyRace){//升级所需灵气
		case "人":
			switch (NowRealm){
				case "无":
					EnergyHelp = 50;
					break;
				case "练气前期":
					EnergyHelp = 100;//*2
					break;
				case "练气中期":
					EnergyHelp = 300;//*3
					break;
				case "练气后期":
					EnergyHelp = 1200;//*4
					break;
					//达到筑基
				case "筑基前期":
					EnergyHelp = 7200;//*6
					break;
				case "筑基中期":
					EnergyHelp = 57600;//*8
					break;
				case "筑基后期":
					EnergyHelp = 576000;//*10
					break;
					//达到金丹
				case "金丹前期":
					EnergyHelp = 8064000;//*14
					break;
				case "金丹中期":
					EnergyHelp = 145152000;//*18
					break;
				case "金丹后期":
					EnergyHelp = 3193344000;//*22
					break;
					//达到元婴
				case "元婴前期":
					EnergyHelp = 95800320000;//*30
					break;
				case "元婴中期":
					EnergyHelp = 3640412160000;//*38
					break;
				case "元婴后期":
					EnergyHelp = 182020608000000;//*50
					break;
				default:
					return "err";
					break;
				}
				break;
			default:alert("升级所需灵力函数出现异常,请直接关闭游戏,不要点退出,直接关闭重新进来!否则数据可能丢失!");
				break;
	}
	return EnergyHelp;
}
//下级名称
function MyRealmSccess(nowRealm){
	var myRace = plus.storage.getItem("Race");
	
	switch(myRace){
		case "人":
			switch (nowRealm){
				case "无":
					return "练气前期";
					break;
				case "练气前期":
					return "练气中期";
					break;
				case "练气中期":
					return "练气后期";
					break;
				case "练气后期":
					return "筑基前期";
					break;
					//达到筑基
				case "筑基前期":
					return "筑基中期";
					break;
				case "筑基中期":
					return "筑基后期";
					break;
				case "筑基后期":
					return "金丹前期";
					break;
					//达到金丹
				case "金丹前期":
					return "金丹中期";
					break;
				case "金丹中期":
					return "金丹后期";
					break;
				case "金丹后期":
					return "元婴前期";
					break;
					//达到元婴
				case "元婴前期":
					return "元婴中期";
					break;
				case "元婴中期":
					return "元婴后期";
					break;//此时准备进入分支了,半步仙魔了,记得设置种族
				default:
					return "err";
					break;
			}
			break;
		default:alert("升级函数出现异常,请直接关闭游戏,不要点退出,直接关闭重新进来!否则数据可能丢失!");
			break;
	}
}
//寿元增加和精力
function MyRealmUpLifeUp(MyRace,NowRealm){
	var LifeUp=1;//寿元增长基数
	switch(MyRace){//升级所需灵气
		case "人":
			switch (NowRealm){
				case "无":
					LifeUp *= 10;
					break;
				case "练气前期":
					LifeUp *= 30;
					break;
				case "练气中期":
					LifeUp *= 35;
					break;
				case "练气后期":
					LifeUp *= 40;//以上倍率为3
					break;
					//达到筑基
				case "筑基前期":
					LifeUp *= 50;
					break;
				case "筑基中期":
					LifeUp *= 60;
					break;
				case "筑基后期":
					LifeUp *= 80;//以上倍率为4
					break;
					//达到金丹
				case "金丹前期":
					LifeUp *= 150;
					break;
				case "金丹中期":
					LifeUp *= 200;
					break;
				case "金丹后期":
					LifeUp *= 300;//以上倍率为6
					break;
					//达到元婴
				case "元婴前期":
					LifeUp *= 500;
					break;
				case "元婴中期":
					LifeUp *= 1000;//以上倍率为9
					break;
				case "元婴后期":
					LifeUp *= 2000;//*10
					break;
				default:
					return "err";
					break;
				}
				break;
			default:alert("升级所需灵力函数出现异常,请直接关闭游戏,不要点退出,直接关闭重新进来!否则数据可能丢失!");
				break;
		}
	return LifeUp;
}
//战力提升
function MyRealmUpDamgeUp(){
	switch(plus.storage.getItem("Race")){
		case "人":
			switch (plus.storage.getItem("MyRealm")){
				case "练气前期":
					plus.storage.setItem("MyVita",String(Number(plus.storage.getItem("MyVita"))+10));
					plus.storage.setItem("MyDamage",String(Number(plus.storage.getItem("MyDamage"))+1));
					break;
				case "练气中期":
					plus.storage.setItem("MyVita",String(Number(plus.storage.getItem("MyVita"))+10));
					plus.storage.setItem("MyDamage",String(Number(plus.storage.getItem("MyDamage"))+1));
					break;
				case "练气后期":
					plus.storage.setItem("MyVita",String(Number(plus.storage.getItem("MyVita"))+10));
					plus.storage.setItem("MyDamage",String(Number(plus.storage.getItem("MyDamage"))+1));
					break;
					//达到筑基
				case "筑基前期":
					plus.storage.setItem("MyVita",String(Number(plus.storage.getItem("MyVita"))+50));
					plus.storage.setItem("MyDamage",String(Number(plus.storage.getItem("MyDamage"))+5));
					plus.storage.setItem("MyDefend",String(Number(plus.storage.getItem("MyDefend"))+1));
					break;
				case "筑基中期":
					plus.storage.setItem("MyVita",String(Number(plus.storage.getItem("MyVita"))+50));
					plus.storage.setItem("MyDamage",String(Number(plus.storage.getItem("MyDamage"))+5));
					plus.storage.setItem("MyDefend",String(Number(plus.storage.getItem("MyDefend"))+1));
					break;
				case "筑基后期":
					plus.storage.setItem("MyVita",String(Number(plus.storage.getItem("MyVita"))+50));
					plus.storage.setItem("MyDamage",String(Number(plus.storage.getItem("MyDamage"))+5));
					plus.storage.setItem("MyDefend",String(Number(plus.storage.getItem("MyDefend"))+1));
					break;
					//达到金丹
				case "金丹前期":
					plus.storage.setItem("MyVita",String(Number(plus.storage.getItem("MyVita"))+200));
					plus.storage.setItem("MyDamage",String(Number(plus.storage.getItem("MyDamage"))+20));
					plus.storage.setItem("MyDefend",String(Number(plus.storage.getItem("MyDefend"))+5));
					plus.storage.setItem("MyEnd",String(Number(plus.storage.getItem("MyEnd"))+1));
					break;
				case "金丹中期":
					plus.storage.setItem("MyVita",String(Number(plus.storage.getItem("MyVita"))+200));
					plus.storage.setItem("MyDamage",String(Number(plus.storage.getItem("MyDamage"))+20));
					plus.storage.setItem("MyDefend",String(Number(plus.storage.getItem("MyDefend"))+5));
					plus.storage.setItem("MyEnd",String(Number(plus.storage.getItem("MyEnd"))+1));
					break;
				case "金丹后期":
					plus.storage.setItem("MyVita",String(Number(plus.storage.getItem("MyVita"))+200));
					plus.storage.setItem("MyDamage",String(Number(plus.storage.getItem("MyDamage"))+20));
					plus.storage.setItem("MyDefend",String(Number(plus.storage.getItem("MyDefend"))+5));
					plus.storage.setItem("MyEnd",String(Number(plus.storage.getItem("MyEnd"))+1));
					break;
					//达到元婴
				case "元婴前期":
					plus.storage.setItem("MyVita",String(Number(plus.storage.getItem("MyVita"))+1000));
					plus.storage.setItem("MyDamage",String(Number(plus.storage.getItem("MyDamage"))+50));
					plus.storage.setItem("MyDefend",String(Number(plus.storage.getItem("MyDefend"))+20));
					plus.storage.setItem("MyEnd",String(Number(plus.storage.getItem("MyEnd"))+5));
					break;
				case "元婴中期":
					plus.storage.setItem("MyVita",String(Number(plus.storage.getItem("MyVita"))+1000));
					plus.storage.setItem("MyDamage",String(Number(plus.storage.getItem("MyDamage"))+50));
					plus.storage.setItem("MyDefend",String(Number(plus.storage.getItem("MyDefend"))+20));
					plus.storage.setItem("MyEnd",String(Number(plus.storage.getItem("MyEnd"))+5));
					break;
				case "元婴后期":
					plus.storage.setItem("MyVita",String(Number(plus.storage.getItem("MyVita"))+1000));
					plus.storage.setItem("MyDamage",String(Number(plus.storage.getItem("MyDamage"))+50));
					plus.storage.setItem("MyDefend",String(Number(plus.storage.getItem("MyDefend"))+20));
					plus.storage.setItem("MyEnd",String(Number(plus.storage.getItem("MyEnd"))+5));
					break;
				default:
					return "err";
					break;
				}
			break;
		default:alert("战力提升函数出现异常,请直接关闭游戏,不要点退出,直接关闭重新进来!否则数据可能丢失!");
			break;
		}
	document.getElementById("MyVita").innerHTML = plus.storage.getItem("MyVita");
	document.getElementById("MyDamage").innerHTML = plus.storage.getItem("MyDamage");
	document.getElementById("MyDefend").innerHTML = plus.storage.getItem("MyDefend");
	document.getElementById("MyEnd").innerHTML = plus.storage.getItem("MyEnd");
}
//突破桎梏
function MyRealmStrongUp(select){
	mui('#sheet1').popover('toggle');
	switch (select){
		case 0://灵气护体
			plus.storage.setItem("MyEnergy","0");
			Odds = Number(plus.storage.getItem("MyEnergy"))/MyRealmUpEnergyHelp(plus.storage.getItem("Strongest"));
			Odds *= 1 + Number(plus.storage.getItem("MyGuts"));
			Odds *= 1 + Number(plus.storage.getItem("SoulStorgest"));//桎梏松动
			if(Math.floor(Math.random()*(MyRealmUpOdds()*2)+1)>Odds){//失败
				plus.storage.setItem("MyAge",String(Number(plus.storage.getItem("MyAge"))-5));
				document.getElementById("MyAge").innerHTML = plus.storage.getItem("MyAge");
				switch (Math.round(Math.random())){
					case 0:
						document.getElementById("tip2").innerHTML = "<b>突破失败,受到重创(寿元-5)</b>";
						break;
					default:
						document.getElementById("tip2").innerHTML = "<b>突破失败,受到重创却不甘(寿元-5)</b>";
						break;
				}
				if(plus.storage.getItem("MyAge")<=0){
					if(plus.storage.getItem("OneDie") == null){
						var d = new Date();
						var x = d.toLocaleDateString();
						plus.storage.setItem("OneDie",x);
					}
					plus.storage.setItem("YouDie","1");
					window.location.href='../SoulWorld/home.html';
				}
			}else{//成功
				plus.storage.setItem("Strongest",MyRealmSccess(plus.storage.getItem("Strongest")));
				document.getElementById("tip2").innerHTML = '<b style="color:red">桎梏突破成功!(境界桎梏提升,道心+1)</b>';
				document.getElementById("Strongest").innerHTML = plus.storage.getItem("Strongest");
			}
			
			break;
		case 1://挡灾金鱼
			Odds = Number(plus.storage.getItem("MyVim"))/(MyRealmUpEnergyHelp(plus.storage.getItem("Strongest"))*10);
			break;
		default:
			break;
	}
}























//发展函数
function MyUpAreaDouble(){//突破领地加成
	var AreaBig = 1 + Number(plus.storage.getItem("MyAreaBig"));//大小
	var AreaHasEnergy = 1 + Number(plus.storage.getItem("MyAreaHasEnergy"));//聚灵
	/* var AreaPlant = Number(plus.storage.getItem("MyAreaPlant"));//灵植
	var AreaMine = Number(plus.storage.getItem("MyAreaMine"));//灵矿
	var AreaBeast = Number(plus.storage.getItem("MyAreaBeast"));//灵兽
	var AreaPulse = Number(plus.storage.getItem("MyAreaPulse"));//灵脉 */
	
	return Math.floor(AreaBig*AreaHasEnergy/100000)
}

function MyEnergyAreaDouble(){//领地灵气加成
	let AreaBig = 1 + Number(plus.storage.getItem("MyAreaBig"));//大小
	
	let AreaPlant = 1 + Number(plus.storage.getItem("MyAreaPlant"));//灵植
	let AreaPulse = 1 + Number(plus.storage.getItem("MyAreaPulse"));//灵脉

	return Math.floor(AreaPulse*AreaPlant/AreaBig/10);
}

function MyAgeAreaDouble(){//寿元倍率
	var AreaPlant = 1 + Number(plus.storage.getItem("MyAreaPlant"));
	var AreaBeast = 1 + Number(plus.storage.getItem("MyAreaBeast"));
	return Math.floor(AreaPlant*AreaBeast/100);
}

function MyMindAreaDouble(){//道心倍率,根据人口
	return 1;
}

function MyPeopleNumberAreaDouble(){//全境界人口总数
	return Number(plus.storage.getItem("FanRen")) + Number(plus.storage.getItem("LianQi")) + Number(plus.storage.getItem("ZhuJi")) + Number(plus.storage.getItem("JinDan")) + Number(plus.storage.getItem("YuanYing"));
}
function MyPeopleAbilityAreaDouble(){//全境人口总战力
	return  Number(plus.storage.getItem("FanRen")) + (Number(plus.storage.getItem("LianQi"))*5) + (Number(plus.storage.getItem("ZhuJi"))*60) + (Number(plus.storage.getItem("JinDan"))*800) + (Number(plus.storage.getItem("YuanYing"))*10000);
}
/* const PeopleAbilityNumber = {
	FanRen : 1,
	LianQi : 5,
	ZhuJi : 60,
	JinDan : 800,
	YuanYing : 10 000
}; */